package com.myhome.controller;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.myhome.config.constant.R;
import com.myhome.domain.CashRecharge;
import com.myhome.domain.CashWithdrawals;
import com.myhome.model.SellCashRechargeParam;
import com.myhome.service.CashWithdrawalsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

/**
 * @Author QYiXin
 * @Date 2021 10 21 08 35
 **/
@RestController
@Api(tags = "cash提现控制器")
@RequestMapping("/cashWithdrawals")
public class CashWithdrawController {
    @Autowired
    private CashWithdrawalsService cashWithdrawalsService;
    @GetMapping("/records")
    @ApiOperation(value = "条件分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "size", value = "每页显示的条数"),
            @ApiImplicitParam(name = "userId", value = "用户的Id"),
            @ApiImplicitParam(name = "userName", value = "用户的名称"),
            @ApiImplicitParam(name = "mobile", value = "用户的手机号"),
            @ApiImplicitParam(name = "status", value = "充值审核状态"),
            @ApiImplicitParam(name = "numMin", value = "充值最小金额"),
            @ApiImplicitParam(name = "numMax", value = "充值最大金额"),
            @ApiImplicitParam(name = "startTime", value = "充值开始时间"),
            @ApiImplicitParam(name = "endTime", value = "充值结束时间"),

    })
    public R<Page<CashWithdrawals>> findByPage(
            @ApiIgnore Page<CashWithdrawals> page,
            Long userId, String userName,
            String mobile, Byte status,
            String numMin, String numMax,
            String startTime, String endTime
    ){
        page.addOrder(OrderItem.desc("lastUpdateTime"));
        Page<CashWithdrawals> result = cashWithdrawalsService.findPages(
                userId,userName,
                mobile,status,
                numMin,numMax,
                startTime,endTime,
                page
        );
        return R.ok(result);
    }


    @GetMapping("/user/records")
    @ApiOperation(value = "查询当前用户的提现记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current",value = "当前页") ,
            @ApiImplicitParam(name = "size",value = "每页显示的大小") ,
            @ApiImplicitParam(name = "status",value = "充值的状态") ,
    })
    public R<Page<CashWithdrawals>> findUserCashRecharge(@ApiIgnore Page<CashWithdrawals> page , Byte status){
        Long userId = Long.valueOf(SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString()) ;
        page.addOrder(OrderItem.desc("lastUpdateTime"));
        Page<CashWithdrawals> cashWithdrawalsPage = cashWithdrawalsService.findCashWithdrawals(page ,userId,status) ;
        return R.ok(cashWithdrawalsPage) ;
    }

    @PostMapping("/sell")
    @ApiOperation(value = "GCN的卖出操作")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "", value = "")
    })
    public R sellCashRecharge(@RequestBody @Validated SellCashRechargeParam sellCashRechargeParam) {
        Long userId = Long.valueOf(SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString());
        Boolean isSell = cashWithdrawalsService.sellCashRecharge(userId,sellCashRechargeParam);
        if (isSell) {
            return R.ok();
        }
        return R.fail("卖出失败");
    }

}
